local skynet = require "skynet"
local tabletool = require "tabletool"
local timetool = require "timetool"
local timer = require "timer"
local base = require "base"
local filelog = require "filelog"


require "hbslenum"

local server = nil
local tablerobothelper = {}

function tablerobothelper.init(service)
	if server == nil then server = service end
end

function tablerobothelper.check_robot_numer()
	local table_data = server.table_data
	local robot_runstate = skynet.call(".robotmgr", "lua", "get_robot_runstate")
	local get_table_robotnum = skynet.call(".robotmgr", "lua", "get_table_robot_num")
	local in_table_robotnum = 0
	local in_table_robot_rids = {}
	for index, seat in ipairs(table_data.seats) do
		if seat.rid ~= 0 and seat.playerinfo.level == 99 then
			in_table_robotnum = in_table_robotnum + 1
			table.insert(in_table_robot_rids, seat.rid)
		end
	end
	if not robot_runstate then
		skynet.send(".robotmgr", "lua", "remove_robots", in_table_robot_rids, in_table_robotnum)
		return 
	end
	if in_table_robotnum == get_table_robotnum then
		---filelog.sys_error("------------robot number is ok-----------")
	else
		if in_table_robotnum > get_table_robotnum then
			local remove_rids = nil
			local leave_robotnum = in_table_robotnum - get_table_robotnum
			if level_robotnum > #in_table_robot_rids then
				level_robotnum = #in_table_robot_rids 
				remove_robots = tabletool.deepcopy(in_table_robot_rids)
			else
				local temp_level_robotnum = leave_robotnum
				remove_rids = {}
				for index, value in ipairs(in_table_robot_rids) do
					if temp_level_robotnum > 0 then
						table.insert(remove_rids, value)
						temp_level_robotnum = temp_level_robotnum - 1
					end
				end
			end
			skynet.send(".robotmgr", "lua", "remove_robots", remove_rids, leave_robotnum)
		elseif in_table_robotnum < get_table_robotnum then
			local enter_robotnum = get_table_robotnum - in_table_robotnum
			skynet.send(".robotmgr", "lua", "add_robot_to_table", skynet.self(), table_data.conf.table_index, enter_robotnum, table_data.conf.min_hb_amount)
		end
	end
end

function tablerobothelper.check_hbinfo_for_robot(will_qiang, limit_time)
	local table_data = server.table_data
	local robot_runstate = skynet.call(".robotmgr", "lua", "get_robot_runstate")
	if not robot_runstate then return end
	local tablelogic = server:get_logicbyname("tablelogic")
	if table_data.conf.play_game_type == HBSLPlayGameType.GAME_TYPE_COMMON then
		local cur_burylandinfo = table_data.cur_landmineinfo
		if next(cur_burylandinfo) ~= nil then
			local now_time = timetool.get_time()
			local left_hb_num = tablelogic.get_left_hb_num()
			local robot_qhb_num = tablelogic.get_qhb_robot_num()
			---filelog.sys_error("-------------will_qiang---------", robot_qhb_num, will_qiang)
			-- if #cur_burylandinfo.wait_qhbinfos > 0 and (table_data.action_to_time - now_time <= 3 and table_data.action_to_time - now_time > 0) 
			-- 	and left_hb_num >= 5 and robot_qhb_num < will_qiang and cur_burylandinfo.total_money <= 100 * 100 then
			if #cur_burylandinfo.wait_qhbinfos > 0 and  robot_qhb_num < will_qiang
				and (table_data.action_to_time - now_time <= limit_time and table_data.action_to_time - now_time > 0) then
				local landmine_num = cur_burylandinfo.landmine_num
				local will_qhbinfo = cur_burylandinfo.wait_qhbinfos[1]
				local will_qhbinfo_hbnum = will_qhbinfo.hb_numbers
				if will_qhbinfo_hbnum ~= landmine_num then
					local no_qhbrobot_rids = {}
					for index, seat in ipairs(table_data.seats) do
						if seat.rid ~= 0 and seat.playerinfo.level == 99 then
							local qhb_info = tablelogic.get_qhb_info(seat.rid)
							if qhb_info == nil then
								table.insert(no_qhbrobot_rids, seat.rid)
							end
						end
					end
					if #no_qhbrobot_rids > 0 then
						local temp = base.get_random(1, #no_qhbrobot_rids)
						skynet.send(".robotmgr", "lua", "do_qhb", {no_qhbrobot_rids[temp]})
					end
				end
			end
		end
	elseif table_data.conf.play_game_type == HBSLPlayGameType.GMAE_TYPE_UNLIMIT then

	end
end

function tablerobothelper.check_hbinfo_for_robot_nolimit()
	local table_data = server.table_data
	local robot_runstate = skynet.call(".robotmgr", "lua", "get_robot_runstate")
	if not robot_runstate then return end
	local tablelogic = server:get_logicbyname("tablelogic")
	if table_data.conf.play_game_type ~= HBSLPlayGameType.GMAE_TYPE_UNLIMIT then return end
	------无限模式使用该函数
	local now_time = timetool.get_time()
	for index, can_qiang_landmine in ipairs(table_data.can_qiang_landmineinfos) do
		local cur_burylandinfo = can_qiang_landmine
		if next(cur_burylandinfo) ~= nil then
			if cur_burylandinfo.is_over == false and now_time < cur_burylandinfo.action_outtime 
				and cur_burylandinfo.already_qhb_num < cur_burylandinfo.total_hb_num then
				local robot_qhb_num = tablelogic.get_qhb_robot_num_nolimit(cur_burylandinfo.hb_id)
				local state, will_qiang = skynet.pcall(skynet.call, ".slotmgr", "lua", "get_q_allhb")
				local de_state, limit_time = skynet.pcall(skynet.call, ".slotmgr", "lua", "get_delaytime")
				if #cur_burylandinfo.wait_qhbinfos > 0 and robot_qhb_num < will_qiang then
					local landmine_num = cur_burylandinfo.landmine_num
					local will_qhbinfo = cur_burylandinfo.wait_qhbinfos[1]
					local will_qhbinfo_hbnum = will_qhbinfo.hb_numbers
					if will_qhbinfo_hbnum ~= landmine_num then
						local no_qhbrobot_rids = {}
						for index, seat in ipairs(table_data.seats) do
							if seat.rid ~= 0 and seat.playerinfo.level == 99 then
								local qhb_info = tablelogic.get_qhb_info_nolimit(cur_burylandinfo.hb_id, seat.rid)
								if qhb_info == nil then
									table.insert(no_qhbrobot_rids, { rid = seat.rid, hb_id = cur_burylandinfo.hb_id })
								end
							end
						end
						if #no_qhbrobot_rids > 0 then
							local temp = base.get_random(1, #no_qhbrobot_rids)
							skynet.send(".robotmgr", "lua", "do_qhb_nolimit", { no_qhbrobot_rids[temp]})
						end
					end
				end
			end
		end
	end
end

function tablerobothelper.check_robot_money()
	--local table_data = server.table_data

end

function tablerobothelper.check_robot_leave()
	local table_data = server.table_data
	local all_robot_rids = {}
	local all_leave_rids = {}
	for index, seat in ipairs(table_data.seats) do
		if seat.rid ~= 0 and seat.playerinfo.level == 99 then
			table.insert(all_robot_rids, seat.rid)
			if seat.already_bury_times > 10 or seat.diamond < 10 * 100 then
				table.insert(all_leave_rids, seat.rid)
			end
		end
	end
	if #all_robot_rids > 0 then
		skynet.send(".robotmgr", "lua", "check_robot_leave", all_robot_rids)
	end

	if #all_leave_rids > 0 then
		skynet.send(".robotmgr", "lua", "robot_leave_table", all_leave_rids)
	end
end

function tablerobothelper.check_mlplayerinfo()
	local table_data = server.table_data
	if table_data.check_mlinfo_timer_id == -1 then
		local noticemsg = {
			table_index = table_data.conf.table_index,
		}
		table_data.check_mlinfo_timer_id = timer.settimer(2*100, "check_mlplayerinfo", noticemsg)
	end
	---判断埋雷人数,如果小于3人，就埋雷
	local tablelogic = server:get_logicbyname("tablelogic")
	local gamename =  tablelogic.get_gamename(table_data)
	local system_inventory = skynet.call(".slotmgr", "lua", "update_system_inventory", gamename)
	---filelog.sys_error("--------check_mlplayerinfo------", system_inventory)
	if tablelogic.get_cur_burynums() >= 3 or system_inventory < - 10000 * 100 then return end

	if table_data.conf.play_game_type == HBSLPlayGameType.GAME_TYPE_COMMON then
		local will_bury_robots = {}
		for index, seat in ipairs(table_data.seats) do
			if seat.rid ~= 0 and seat.playerinfo.level == 99 then
				---local bury_money = base.get_random(HBSLRobotBuryMoney.BURY_MIN_MONEY,HBSLRobotBuryMoney.BURY_MAX_MONEY) * 10
				local bury_money = skynet.call(".slotmgr", "lua", "get_bury_money")
				if seat.diamond > bury_money and not tablelogic.is_already_bury(seat.rid) and #will_bury_robots <3 then
					table.insert(will_bury_robots, { rid = seat.rid, bury_money = bury_money})
				end
			end
		end
		if #will_bury_robots > 0 then
			skynet.send(".robotmgr", "lua", "do_ml", will_bury_robots, table_data.conf.hb_nums)
		end
	elseif table_data.conf.play_game_type == HBSLPlayGameType.GMAE_TYPE_UNLIMIT then
		local real_player_num = tablelogic.get_real_playernum()
		local will_bury_robots = {}
		for index, seat in ipairs(table_data.seats) do
			if seat.rid ~= 0 and seat.playerinfo.level == 99 then
				local bury_money = skynet.call(".slotmgr", "lua", "get_bury_money")
				local can_bury_num = math.floor(seat.diamond / bury_money)
				local bury_num = base.get_random(1, can_bury_num)
				if seat.diamond > bury_money * bury_num and not tablelogic.is_already_bury(seat.rid) and #will_bury_robots < 5 then
					table.insert(will_bury_robots, { rid = seat.rid, bury_money = bury_money, bury_num = bury_num})
				end
			end
		end
		if #will_bury_robots > 0 and real_player_num > 0 then
			skynet.send(".robotmgr", "lua", "do_ml_nolimit", will_bury_robots)
		end	
	end
end


function tablerobothelper.check_cancel_bury()
	local table_data = server.table_data
	local all_robot_rids = {}
	local cancel_bury_robot_rids = {}
	for index, seat in pairs(table_data.seats) do
		if seat.rid ~= 0 and seat.playerinfo.level == 99 and seat.no_curbury_round >= 10 then
			table.insert(cancel_bury_robot_rids, seat.rid)
		end
	end
	if #cancel_bury_robot_rids > 0 then
		skynet.send(".robotmgr", "lua", "do_cancel_bury", cancel_bury_robot_rids)
	end
end

return tablerobothelper